<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/3/16 0016
 * Time: 17:27
 */
namespace app\admin\controller;
use app\models\Models;

class User extends Check{
    /**
     * 用户列表
     * @return \think\response\View
     */
    public function userList(){
        $get = input('param.');
        $user = new Models('user');
        $type = new Models('userType');
        //$vocation = new Models('vocationType');
        // 条件查询
        $search = search($get,'a.username,a.nickname','a.create_time','a.status');
        $where  = $search['condition'];
        // 根据用户类型查询
        if($get['type_id']){
            $where .= ' and a.type_id = '.$get['type_id'];
            $search['display']['type_id'] = $get['type_id'];
        }
        /*// 根据情感状态查询
        if($get['emotion']){
            $where .= ' and a.emotion = '.$get['emotion'];
            $search['display']['emotion'] = $get['emotion'];
        }
        // 根据职业类型查询
        if($get['vocation_id']){
            $where .= ' and a.vocation_id = '.$get['vocation_id'];
            $search['display']['vocation_id'] = $get['vocation_id'];
        }*/
        // 商家列表查询下属艺人列表
        if($get['business_actor_id']){
            $where .= ' and a.up_id = '.$get['business_actor_id'].' and a.type_id = '.Models::USER_TYPE_ACTOR;
            $search['display']['business_id'] = $get['business_actor_id'];
        }
        // 商家列表查询下属销售列表
        if($get['business_seller_id']){
            $where .= ' and a.up_id = '.$get['business_seller_id'].' and a.type_id = '.Models::USER_TYPE_SELLER;
            $search['display']['business_id'] = $get['business_seller_id'];
        }
        $join   = [
            ['user_type b','a.type_id = b.id','left'],
            ['account c','a.id = c.uid','left'],
            ['business d','a.up_id = d.id','left'],
            ['vocation_type e','a.vocation_id = e.id','left'],
        ];
        $field  = "a.*,b.name,c.balance,c.coin_balance,e.name vocation";
        $list = $user->_page($where,$field,'a.id desc',$join,$get['page'],15,$get);
        $userType = $type->_select(['status'=>Models::STATUS_ON]);
        //$vocationType = $vocation->_select(['status'=>Models::STATUS_ON]);
        return view('userList',['list'=>$list->items(),'page'=>$list->render(),'display'=>$search['display'],'type'=>$userType/*,'vocation'=>$vocationType*/]);
    }

    /**
     * 添加用户
     * @return array|\think\response\View
     */
    public function userAdd(){
        if(request()->isAjax()){
            $post = input('param.');
            $user = new Models('user');
            $account = new Models('account');
            try{
                // 添加场景验证
                $validate = $this->validate($post,'User.userAdd');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                $post['actor_id'] = $post['type_id'] == Models::USER_TYPE_ACTOR ? $post['actor_id'] : 0;
                $post['password'] = password_hash('123456',true);
                $post['create_time'] = time();
                $uid  = $user->_add($post);
                if(!$uid){
                    throw new \LogicException('操作失败',1010);
                }
                $bool = $account->_add(['uid'=>$uid]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $type  = new Models('userType');
        $admin = new Models('admin');
        $actor = new Models('user_actor_type');
        // 用户类型列表
        $user_type = $type->_select(['status'=>Models::STATUS_ON,'id'=>['<>',Models::USER_TYPE_NORMAL]]);
        // 商家列表
        $business  = $admin->joinMany([['business b','a.id = b.id','right']],['a.status'=>Models::STATUS_ON],'a.id,b.name');
        // 艺人类型列表
        $actor_list = $actor->_select(['status'=>Models::STATUS_ON]);
        return view('userAdd',['type'=>$user_type,'business'=>$business,'actor'=>$actor_list]);
    }

    /**
     * 修改用户
     * @return array|\think\response\View
     */
    public function userSave(){
        $user = new Models('user');
        if(request()->isAjax()){
            $post = input('param.');
            try{
                // 添加场景验证
                $validate = $this->validate($post,'User.userSave');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                $bool = $user->_save($post);
                if(false === $bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $get   = input('param.');
        $type  = new Models('userType');
        $admin = new Models('admin');
        $info = $user->_find($get['id']);
        // 用户类型列表
        $user_type = $type->_select(['status'=>Models::STATUS_ON]);
        // 商家列表
        $business  = $admin->joinMany([['business b','a.id = b.id','right']],['a.status'=>Models::STATUS_ON],'a.id,b.name');
        return view('userSave',['type'=>$user_type,'info'=>$info,'business'=>$business]);
    }

    /**
     * 用户详情
     * @return \think\response\View
     */
    public function userShow(){
        $get = input('param.');
        $user = new Models('user');
        $join = [
            ['user_type b','a.type_id = b.id','left'],
            ['account c','a.id = c.uid','left'],
        ];
        $field = 'a.*,b.name,c.balance,c.amount,c.coin_balance,c.coin_income';
        $info  = $user->joinOne($join,['a.id'=>$get['id']],$field);
        $config = self::getConfig();
        $info['portrait'] = $config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'portrait'. DS .'s'.$info['portrait'];
        return view('userShow',['info'=>$info]);
    }

    /**
     * 启用
     * @return array
     */
    public function start(){
        if(request()->isAjax()){
            $post = input('param.');
            $user = new Models('user');
            try{
                $bool = $user->setValue(['id'=>$post['id']],['status'=>$user::STATUS_ON]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 停用
     * @return array
     */
    public function stop(){
        if(request()->isAjax()){
            $post = input('param.');
            $user = new Models('user');
            try{
                $bool = $user->setValue(['id'=>$post['id']],['status'=>$user::STATUS_OFF]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 重置用户密码
     * */
    public function repassword(){
        if(request()->isAjax()){
            $user = new Models('user');
            $post = input('param.');
            try{
                $password = password_hash('123456',true);
                $bool = $user->setValue(['id'=>$post['id']],['password'=>$password]);
                if(false === $bool){
                    throw new \LogicException("操作失败",1010);
                }
            }catch(\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }
}